#include "cScene.h"
#include "Globals.h"
#include "stdio.h"
#include <iostream>


cScene::cScene(void)
{
}

cScene::~cScene(void)
{
}

bool cScene::LoadRoom(int room)
{
	bool res;
	FILE *fd;
	char file[20];
	int i, j, px, py;
	int i_sprites, j_sprites;
	int tile;
	int coordx_tile, coordy_tile;
	float rcoordx_tile, rcoordy_tile;
	int count_sprites;
	char char_reader;
	int char_counter;
	int n0, n1, n2, n3;
	int a=0;

	res = true;

	if (room<10) sprintf(file, "Assets\\%s0%d%s", (char *)FILENAME, room, (char *)FILENAME_EXT);
	else		 sprintf(file, "Assets\\%s%d%s", (char *)FILENAME, room, (char *)FILENAME_EXT);

	fd = fopen(file, "r");
	if (fd == NULL) return false;

	m_idDisplayList = glGenLists(1);
	glNewList(m_idDisplayList, GL_COMPILE);
		glBegin(GL_QUADS);

			//std::cout << "Entrar FOR j =SCENE_HEIGHT" << std::endl;

			for (j = SCENE_HEIGHT-1; j >= 0; j--)
			{

				px = SCENE_Xo ;
				py = SCENE_Yo + (j*TILE_SIZE);
				
				//std::cout << "py=" << py << std::endl;
				//std::cout << "j=" << j << std::endl;

				//std::cout << "Entrar FOR i =SCENE_WHIDTH" << std::endl;
				for (i = 0; i<SCENE_WIDTH; i++)
				{
					//std::cout << "i=" << i << std::endl;
					//std::cout << "px=" << px << std::endl;

					char_counter = 3;
					n0, n1, n2, n3 = 0;
					fscanf(fd, "%c", &char_reader);
					//if (j == 0 && i == 0){ std::cout << "Entrar while char_reader" << std::endl; }
					while (char_reader != ',')
					{
						if (char_counter == 0)				{ n0 = char_reader - '0'; tile = n0 + n1 * 10 + n2 * 100 + n3 * 1000; }
						else if (char_counter == 1)			{ n1 = char_reader - '0'; tile = n1 + n2 * 10 + n3 * 100;				char_counter--; }
						else if (char_counter == 2)			{ n2 = char_reader - '0'; tile = n2 + n3 * 10;							char_counter--; }
						else if (char_counter == 3)			{ n3 = char_reader - '0'; tile = n3;									char_counter--; }
						fscanf(fd, "%c", &char_reader);
					}
					//if (j == 0 && i == 0){ std::cout << "Surt while char_reader" << std::endl; }
					mp_map[(j*SCENE_WIDTH) + i].SetInfo(tile);

					//Calcul coordenades del tile

					count_sprites = 0;
					coordy_tile = 0;
					coordx_tile = 0;
					j_sprites = 54;

					while (j_sprites >= 0 ^ count_sprites==tile)
					{	
						coordy_tile = 54-j_sprites;
						i_sprites = 0;
															
						while (i_sprites <= 54 ^ count_sprites==tile)
						{
							count_sprites++;
							coordx_tile = i_sprites;
							i_sprites++;
						}
													
					j_sprites--;
					}
					bool solid = (tile != 0);
					mp_map[(j*SCENE_WIDTH) + i].SetCollider(new Collider(px, py, px + BLOCK_SIZE, py + BLOCK_SIZE, solid));
					//mp_map[(j*SCENE_WIDTH) + i].GetCollider()->SetCollisions(tile == 0);
										
					//BLOCK_SIZE = 16, FILE_SIZE=936 r=0,0171

					if (count_sprites == 0){}
					else if (count_sprites != 0)
					{
						rcoordx_tile = (1.0f + (1.0f + 16.0f)*(float)coordx_tile) / 936.0f; 
						rcoordy_tile = (1.0f + (1.0f + 16.0f)*(float)coordy_tile) / 936.0f; 

						
						glTexCoord2f(rcoordx_tile,			rcoordy_tile + 0.0171);		glVertex2i(px, py);
						glTexCoord2f(rcoordx_tile + 0.0171, rcoordy_tile + 0.0171);		glVertex2i(px + BLOCK_SIZE, py);
						glTexCoord2f(rcoordx_tile + 0.0171, rcoordy_tile);				glVertex2i(px + BLOCK_SIZE, py + BLOCK_SIZE);
						glTexCoord2f(rcoordx_tile,			rcoordy_tile);				glVertex2i(px, py + BLOCK_SIZE);

					}
					
					px += TILE_SIZE;
			
				}
				//std::cout << "Surt FOR i =SCENE_WHIDTH" << std::endl;
				
				fscanf(fd, "%c", &char_reader); //pass enter
			}
			//std::cout << "Entrar FOR j =SCENE_HEIGHT" << std::endl;
			
		glEnd();
	glEndList();

	fclose(fd);

	return res;
}

void cScene::Draw(int tex_id)
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,tex_id);
	glCallList(m_idDisplayList);
	glDisable(GL_TEXTURE_2D);
}
Tile* cScene::GetMap()
{
	return mp_map;
}

std::pair <std::pair<int,int> , std::pair<int,int>> cScene::GetDoor(int x, std::string string)

{
	std::pair <std::pair<int, int>, std::pair<int, int>> vector;
	bool res;
	if (string == "Room01")
	{
		if (x == 1){ vector.first.first = 1 * 16; vector.first.second = 16 * 29; vector.second.first = 16 * 3; vector.second.second = 16 * 34; }
		else if (x == 2){ vector.first.first = 16*44; vector.first.second = 16*30; vector.second.first = 16*47; vector.second.second = 16*34; }
		else{}
	}


	else if (string == "Room02")
	{
		vector.first.first = 25 * 16; vector.first.second = 16 * 4; vector.second.first = 16 * 29; vector.second.second = 16 * 7;
	}
	else if (string == "Room03")
	{
		vector.first.first = 44 * 16; vector.first.second = 16 * 23; vector.second.first = 16 * 47; vector.second.second = 16 * 31;
	}
	else if (string == "Room04")
	{
		vector.first.first = 2 * 16; vector.first.second = 16 * 5; vector.second.first = 16 * 6; vector.second.second = 16 * 7;
	}
	else if (string == "Room05")
	{
		vector.first.first = 43 * 16; vector.first.second = 16 * 4; vector.second.first = 16 * 45; vector.second.second = 16 * 9;
	}

	return vector;

}